package com.example.clientsideapplication;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

public class ModifyOfferService extends IntentService {

	private Socket socket;
	private ObjectOutputStream outputWriter;
	private SharedPreferences preferences;
	private String serverAddress;
	private int serverSocket;
	private final static String MY_LOGIN = "myAppLogIn";

	public ModifyOfferService() {
		super("ModifyOfferService");
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		// TODO Auto-generated method stub

		Offer offer = (Offer) intent.getSerializableExtra("offer");
		Integer idOffer = intent.getIntExtra("idOffer", -1);
		
		serverAddress = intent.getStringExtra("serverAddress");
		serverSocket = intent.getIntExtra("serverSocket", 21111);
		
		

		try {
			socket = new Socket(serverAddress, serverSocket);

			outputWriter = new ObjectOutputStream(socket.getOutputStream());

			preferences = getSharedPreferences(MY_LOGIN, Context.MODE_PRIVATE);
			Integer idMerchant = preferences.getInt("idMerchant", -1);

			Entry<Integer, Integer> entry = new SimpleEntry<Integer, Integer>(
					idMerchant, idOffer);
			Entry<Entry<Integer, Integer>, Offer> output = new SimpleEntry<Entry<Integer, Integer>, Offer>(
					entry, offer);

			DataPackage data = new DataPackage(2, output);

			outputWriter.writeObject(data);

		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
